package com.ycz.service.Impl;/*
 @author ycz
 @date 2021-06-26-15:45
*/

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ycz.annotation.RedisAnnotation;
import com.ycz.annotation.UpdateAnnotation;
import com.ycz.common.CodeMsg;
import com.ycz.common.Constant;
import com.ycz.common.Result;
import com.ycz.form.BusCustomerForm;
import com.ycz.mapper.BusCustomerMapper;
import com.ycz.query.BusCustomerQuery;
import com.ycz.service.BusCustomerService;
import com.ycz.utils.DateUtil;
import com.ycz.vo.BusCustomerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@Service
public class BusCustomerServiceImpl implements BusCustomerService {

    @Autowired
    private BusCustomerMapper busCustomerMapper;
    //传回去是Result

    @RedisAnnotation(name = Constant.REDIS_CUSTOMER)
    public Result selectCustomList(BusCustomerQuery query) {

        Page<BusCustomerVo> busCustomerVoPage= PageHelper.startPage(query.getPage(),query.getLimit());

        busCustomerMapper.selectCustomList(query);

        System.out.println(busCustomerVoPage.toPageInfo());

        return new Result(busCustomerVoPage.toPageInfo());
    }

    @Override
    @UpdateAnnotation(name = Constant.REDIS_CUSTOMER)
    public Result updateCustomer(BusCustomerForm busCustomerForm) {
        BusCustomerVo busCustomerVo= busCustomerMapper.selectCustomer(busCustomerForm);
        if (busCustomerVo!=null){
            return new Result(CodeMsg.MESSAGE_EQUALS_NEWMESSAGE);
        }
        /*
        根据身份证号查询
         */
        BusCustomerQuery query=new BusCustomerQuery();
        query.setIdCard(busCustomerForm.getIdCard());
        busCustomerVo=busCustomerMapper.selectCustomer_idCard_phone(query);
        if (busCustomerVo!=null && !busCustomerForm.getId().equals(busCustomerVo.getId())){
            return new Result(CodeMsg.CUSTOMER_ID_CARD_EXIST_ERROR);
        }
        /*
        根据手机号查询是否重复
         */
        query=new BusCustomerQuery();
        query.setPhone(busCustomerForm.getPhone());
        busCustomerVo=busCustomerMapper.selectCustomer_idCard_phone(query);
        if (busCustomerVo!=null && !busCustomerForm.getId().equals(busCustomerVo.getId())){
            return new Result(CodeMsg.CUSTOMER_PHONE_EXIST_ERROR);
        }

        busCustomerForm.setUpdateTime(DateUtil.getDate());
        busCustomerMapper.updateCustomer(busCustomerForm);
        return new Result();
    }

    @Override
    @UpdateAnnotation(name = Constant.REDIS_CUSTOMER)
    public Result addCustomer(BusCustomerForm busCustomerForm) {

        /*
        要搜索的东西
         */
        BusCustomerQuery query=new BusCustomerQuery();

        BusCustomerVo busCustomerVo=new BusCustomerVo();

        query.setPhone(busCustomerForm.getPhone());

        busCustomerVo=busCustomerMapper.selectCustomer_idCard_phone(query);
        if (busCustomerVo!=null){
            return new Result(CodeMsg.CUSTOMER_PHONE_EXIST_ERROR);
        }
        query=new BusCustomerQuery();
        query.setIdCard(busCustomerForm.getIdCard());
        busCustomerVo=busCustomerMapper.selectCustomer_idCard_phone(query);
        if (busCustomerVo!=null){
            return new Result(CodeMsg.CUSTOMER_ID_CARD_EXIST_ERROR);
        }


        busCustomerForm.setCreateTime(DateUtil.getDate());
        busCustomerForm.setUpdateTime(DateUtil.getDate());
        busCustomerMapper.insertBusCustomer(busCustomerForm);
        return new Result();
    }

    @Override
    @UpdateAnnotation(name = Constant.REDIS_CUSTOMER)
    public Result deleteCustomer(Integer id) {
        busCustomerMapper.deleteCustomer(id);
        return new Result();
    }

    @Override
    public void exportCustomer(BusCustomerQuery query, HttpServletResponse response) throws IOException {
        //与客户端相关的输出流
        ServletOutputStream os=response.getOutputStream();
        //response为HttpServletResponse对象
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
        String fileName= URLEncoder.encode("客户信息.xls","utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+fileName);
        //符合条件的客户
       List<BusCustomerVo> busCustomerVoList= busCustomerMapper.selectCustomList(query);
        //将客户转化为excel数据流
        // 通过工具类创建writer，默认创建xls格式
        ExcelWriter writer = ExcelUtil.getWriter();
        //取别名
        writer.addHeaderAlias("id","编号");
        writer.addHeaderAlias("name","姓名");
        writer.addHeaderAlias("phone","手机号码");
        writer.addHeaderAlias("idCard","身份证");
        writer.addHeaderAlias("sex","性别");
        writer.addHeaderAlias("address","地址");
        writer.addHeaderAlias("createTime","创建时间");
        writer.addHeaderAlias("updateTime","更新时间");
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(busCustomerVoList,true);
        //将Excel输出给浏览器
        writer.flush(os,true);


    }
    /*
    处理上传的文件Excel中客户文件
    将Excel信息中客户插入到数据库中
     */
    @Override
    @UpdateAnnotation(name = Constant.REDIS_CUSTOMER)
    public Result batchAddCustomer(MultipartFile file) throws IOException {
        //解析excel文件
       ExcelReader render= ExcelUtil.getReader(file.getInputStream());
        //将数据转化为对象
        List<BusCustomerVo> busCustomerVoList=render.readAll(BusCustomerVo.class);
        busCustomerMapper.batchInsert(busCustomerVoList);
        System.out.println(busCustomerVoList);
        return new Result();
    }
}
